package cn.yzl.okhttputils.okhttp.callback;

import cn.yzl.okhttputils.okhttp.request.RequestCall;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public abstract class Callback<T> {
    protected RequestCall requestCall;

    /**
     * 再次执行本请求标记
     */
    protected boolean requestAgain;

    public Callback() {
        requestAgain = true;
    }

    public Callback(boolean requestAgain) {
        this.requestAgain = requestAgain;
    }

    /**
     * UI Thread
     *
     * @param request
     */
    public void onBefore(Request request) {
    }

    /**
     * UI Thread
     *
     * @param
     */
    public void onAfter() {
    }

    /**
     * UI Thread
     *
     * @param progress
     */
    public void inProgress(float progress) {

    }

    /**
     * Thread Pool Thread
     *
     * @param response
     */
    public abstract T parseNetworkResponse(Response response) throws Exception;

    public abstract void onError(Call call, int code,Exception e);

    public abstract void onResponse(T response);


    public static Callback CALLBACK_DEFAULT = new Callback() {

        @Override
        public Object parseNetworkResponse(Response response) throws Exception {
            return null;
        }

        @Override
        public void onError(Call call, int code,Exception e) {

        }

        @Override
        public void onResponse(Object response) {

        }
    };

    public void setRequestCall(RequestCall requestCall) {
        this.requestCall = requestCall;
    }

    public boolean isRequestAgain() {
        return requestAgain;
    }
    /**
     * 再次执行本请求
     */
    public void callSelf() {
        if (requestCall != null
                && requestAgain) {
            requestCall.execute(this);
        }
    }
}